/**
 * Author :tianhongyu
 * Date : 2018/5/8 15:57
 */
package com.tian.source.controller;

import com.tian.core.beans.ResultBean;
import com.tian.core.page.Page;
import com.tian.source.model.Document;
import com.tian.source.model.vo.DocumentVo;
import com.tian.source.service.DocumentService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    private String filePath = null;

    /**
     * 上传文件
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public  ResultBean<String> upload(MultipartFile file, HttpServletRequest request)throws Exception{
        if (file != null){
            String fileName = file.getOriginalFilename();
            File files = new File(setFilePath(request), System.currentTimeMillis() + fileName);
            file.transferTo(files);
            filePath = "/document/" + files.getName();
        }
        return new ResultBean<>("上传成功");
    }
    /**
     * 生成路径
     *
     * @param request
     * @return
     */
    public File setFilePath(HttpServletRequest request) {
        File file = new File("G:");
        File dir = new File(file.getPath(), "document");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir;
    }

    /**
     * 添加文档
     * @param document
     * @return
     */
    @PostMapping("/addDocument")
    @ResponseBody
    public ResultBean<String> add(Document document){
        document.setFile(filePath);
        document.setCreateTime(new Date());
        documentService.add(document);
        return new ResultBean<>("添加成功");
    }

    @GetMapping("/getAllByPage")
    @ResponseBody
    public ResultBean<Page<List<DocumentVo>>> getAllByPage(Integer pageNum,Integer pageSize,String start,String end){
        Page page = new Page(pageNum,pageSize);
        return new ResultBean<>(documentService.findAllByPage(page,start,end));
    }

    @GetMapping("/download")
    public ResponseEntity<byte[]> down(Integer id) throws Exception {
        Document document = documentService.findById(id);
        //设置文件路径
        String realPath = "G:";
        File file = new File(realPath,document.getFile());
        if (file.exists()){
            HttpHeaders headers = new HttpHeaders();
            // 设置下载文件的描述信息
            headers.setContentDispositionFormData("attchment", file.getName());
            // 设置响应内容流--二进制
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
        }else {
            return null;
        }
    }
}
